﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BuscadorDePreco.DataContract;
using System.Transactions;

namespace BuscadorDePreco.Data
{
    public class MarcaTableAdapter
    {

        public int AdicionarMarca(MarcaData marca)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            int id = -1;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    Marca m = MarcaData.ToMarcaObj(marca);

                    buscadorEntities.Marca.AddObject(m);
                    buscadorEntities.SaveChanges();

                    id = m.Id;
                }
                scope.Complete();
            }

            return id;
        }

        public List<MarcaData> RecuperarMarcas()
        {
            List<MarcaData> marcaList = new List<MarcaData>();

            using (BuscadorEntities buscadorEntities = new BuscadorEntities())
            {
                var marcas = (from emp in buscadorEntities.Marca
                                select emp).ToList();

                foreach (Marca marca in marcas)
                    marcaList.Add(MarcaData.ToMarcaData(marca));
            }

            return marcaList;
        }

        public void ExcluirMarca(int id)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    var marcaObj = (from marca in buscadorEntities.Marca
                                    where marca.Id == id
                                    select marca).ToList();

                    if (marcaObj != null && marcaObj.Count > 0)
                    {
                        buscadorEntities.Marca.DeleteObject(marcaObj.First());
                        buscadorEntities.SaveChanges();
                    }
                }
                scope.Complete();
            }
        }

        public MarcaData RecuperaMarca(int id)
        {
            using (BuscadorEntities buscadorEntities = new BuscadorEntities())
            {
                var marcas = (from marca in buscadorEntities.Marca
                              where marca.Id == id
                              select marca);

                if (marcas != null && marcas.Count() > 0)
                    return MarcaData.ToMarcaData(marcas.First());
            }

            return null;
        }

        public void AtualizarMarca(MarcaData newData)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    var marca = from marc in buscadorEntities.Marca
                                   where marc.Id == newData.Id
                                   select marc;

                    if (marca != null && marca.Count() > 0)
                    {
                        Marca m = marca.First();
                        m.Nome = newData.Nome;
                    }

                    buscadorEntities.SaveChanges();

                }
                scope.Complete();
            }
        }
    }
}